/**
 * @ProjectName:exam
 * @FileName: DriverEntityServiceImpl.java
 * @PackageName: edu.xpu.project.user.service
 * @Company:北京有生博大软件有限公司（西安分公司）
 * @Copyright (c) 2017,RiseSoft  All Rights Reserved.
 * @date 2017年4月29日 下午6:23:15
 */
package edu.xpu.project.user.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.xpu.project.user.entity.DriverEntity;
import edu.xpu.project.user.repository.DriverRepository;

/**
 * @ClassName: DriverEntityServiceImpl.java
 * @Description: DriverEntityService的一个业务接口实现
 *
 * @author liukang
 * @date 2017年4月29日 下午6:23:15
 * @version 
 * @since JDK 1.6
 */
@Service
@Transactional(readOnly=true)
public class DriverEntityServiceImpl implements DriverEntityService {
	
	@Autowired
	private DriverRepository driverRepository;

	@Override
	public DriverEntity get(Long id) {
		return driverRepository.findOne(id);
	}

	@Transactional(readOnly=false)
	@Override
	public DriverEntity saveOrUpdate(DriverEntity t) {
		return driverRepository.saveAndFlush(t);
	}

	@Transactional(readOnly=false)
	@Override
	public void delete(Long id) {
		driverRepository.delete(id);
	}

	@Override
	public Page<DriverEntity> getPageByCriterias(String pageNumberStr, String pageSizeStr, final DriverEntity driverEntity) {
		Integer pageNumber = 1;
		Integer pageSize = 10;
		try {
			pageNumber = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "createDate"));
		return driverRepository.findAll(new Specification<DriverEntity>() {
			
			@Override
			public Predicate toPredicate(Root<DriverEntity> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				//添加用户的查询条件
				List<Predicate> predicates = new ArrayList<Predicate>(2);
				//姓名
				if (StringUtils.isNotBlank(driverEntity.getName())) {
					predicates.add(builder.like(root.get("name").as(String.class), "%"+driverEntity.getName()+"%"));
				}
				//手机号
				if (StringUtils.isNotBlank(driverEntity.getTelPhone())) {
					predicates.add(builder.like(root.get("telPhone").as(String.class), "%"+driverEntity.getTelPhone()+"%"));
				}
				
				if (predicates.size() == 0) {
					return null;
				}
				Predicate[] predArr= new Predicate[predicates.size()];
				return builder.and(predicates.toArray(predArr));
			}
			
		}, pageable);
	}

	@Override
	public List<DriverEntity> findAll() {
		return driverRepository.findAll();
	}

}

